package cn.appsys.controller.developer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.appsys.pojo.DevUser;
import cn.appsys.service.DevUserService;
import cn.appsys.tools.Constants;

@Controller
@RequestMapping("dev")
public class DevUserController {

	@Resource
	public DevUserService devUserService;

	/**
	 * 登录页面
	 * 
	 * @return
	 */
	@RequestMapping("login")
	public String login() {
		return "devlogin";
	}

	/**
	 * 开发者登录
	 * 
	 * @return
	 */
	@RequestMapping("dologin")
	public String doLogin(String devCode, String devPassword, Model model, HttpSession session,HttpServletRequest request) {
		DevUser devUser = devUserService.login(devCode, devPassword);
		if (devUser != null) {
			request.getSession().setAttribute(Constants.USER_SESSION, devUser);
			session.setAttribute("userSession", devUser);
			return "developer/main";
		} else {
			model.addAttribute("error", "用户名或密码不正确");
			return "devlogin";
		}
	}

	/**
	 * 注销用户
	 * 
	 * @return
	 */
	@RequestMapping("logout")
	public String logOut(Model model, HttpSession session) {
		session.removeAttribute("userSession");
		return "devlogin";
	}

}
